﻿using log4net;
using server.Business;
using server.Core;
using server.DAO;
using server.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace server.Tool
{
    public class SeverTimer
    {

        private ILog Logger = log4net.LogManager.GetLogger("server.Tool.SeverTimer");
        //推送地图
        private int IntervalSendPostion = 1000 * 3;
        private System.Threading.Timer tmrsendPostion = null;


        //推送背包物资
        private int IntervalSendKnapsack = 1000 * 1;
        private System.Threading.Timer tmrsendKnapsack = null;

        //拾取背包物资检查
        private int IntervalCheckKnapsackPick = 1000 * 1;
        private System.Threading.Timer tmrIntervalCheckKnapsackPick = null;


        //检查在线连接
        private int IntervalCheckConnect = 1000 * 10;
        private System.Threading.Timer tmrCheckConnect = null;


        //检查电子围栏
        private int IntervalCheckFence = 1000 * 3;
        private System.Threading.Timer tmrCheckCFence = null;


        private EditGrounpDao editGrounpDao = new EditGrounpDao();

        public void Init()
        {
            tmrsendPostion = new System.Threading.Timer(SendMapDataCallBack, null, IntervalSendPostion, IntervalSendPostion);
            tmrsendKnapsack = new System.Threading.Timer(SendKnapsackDataCallBack, null, IntervalSendKnapsack, IntervalSendKnapsack);
            tmrCheckConnect = new System.Threading.Timer(CheckConnectCallBack, null, IntervalCheckConnect, IntervalCheckConnect);
            tmrCheckCFence = new System.Threading.Timer(CheckFenceCallBack, null, IntervalCheckFence, IntervalCheckFence);
            tmrIntervalCheckKnapsackPick = new System.Threading.Timer(CheckKnapsackPickCallBack, null, IntervalCheckKnapsackPick, IntervalCheckKnapsackPick);
            MapDataPushHandle.Instance.Init();

        }

        /// <summary>
        /// 定时发送位置数据
        /// </summary>
        /// <param name="state"></param>
        private void SendMapDataCallBack(object state)
        {
            try
            {
                tmrsendPostion.Change(Timeout.Infinite, Timeout.Infinite);
                MapDataPushHandle.Instance.RealPushMapData();
            }
            catch(Exception e)
            {
                Logger.Error( e.Message);
                Logger.Error(e.StackTrace);
                tmrsendPostion.Dispose();
                tmrsendPostion = new System.Threading.Timer(SendMapDataCallBack, null, IntervalSendPostion, IntervalSendPostion);
                Console.WriteLine("位置更新异常信息：" + e.Message);
            }
            finally
            {
                tmrsendPostion.Change(IntervalSendPostion, IntervalSendPostion);
            }
        }

        /// <summary>
        /// 定时检查连接
        /// </summary>
        /// <param name="state"></param>
        private void CheckConnectCallBack(object state)
        {
            try
            {
                tmrCheckConnect.Change(Timeout.Infinite, Timeout.Infinite);
                ConcurrentDictionary<PubgSession, SessionItem> dic = PubgSession.mOnLineConnections;
                if(dic==null || dic.Count==0)
                {
                  //  Console.WriteLine("当前服务器连接数量：" + dic.Count);
                    tmrCheckConnect.Change(IntervalCheckConnect, IntervalCheckConnect);
                    return;
                }

                SessionMsg.RemoveTimeOutConnectSession();
               // Console.WriteLine("当前服务器连接数量：" + dic.Count);
            }
            catch(Exception e)
            {
                Logger.InfoFormat("连接异常：：{0}", e.Message);
                tmrCheckConnect.Dispose();
                tmrCheckConnect = new System.Threading.Timer(CheckConnectCallBack, null, IntervalCheckConnect, IntervalCheckConnect);
               
                Console.WriteLine("连接异常信息：" +  e.Message);
            }
            finally
            {
                tmrCheckConnect.Change(IntervalCheckConnect, IntervalCheckConnect);
            }
        }

        /// <summary>
        /// 检查电子围栏，并缩小包围圈
        /// </summary>
        /// <param name="state"></param>
        private void CheckFenceCallBack(object state)
        {
            try
            {
                tmrCheckCFence.Change(Timeout.Infinite, Timeout.Infinite);
                editGrounpDao.UpdateFenceScope(IntervalCheckFence/1000);

            }
            catch (Exception e)
            {
                Logger.InfoFormat("电子围栏定时异常：：{0}", e.Message);
                tmrCheckConnect.Dispose();
                tmrCheckCFence = new System.Threading.Timer(CheckFenceCallBack, null, IntervalCheckFence, IntervalCheckFence);
                Console.WriteLine("电子围栏定时异常：" + e.Message);
            }
            finally
            {
                tmrCheckCFence.Change(IntervalCheckFence, IntervalCheckFence);
            }
        }

        #region Knapsack

        /// <summary>
        /// 检查拾取背包物资
        /// </summary>
        /// <param name="state"></param>
        private void CheckKnapsackPickCallBack(object state)
        {
            try
            {
                tmrIntervalCheckKnapsackPick.Change(Timeout.Infinite, Timeout.Infinite);
                KnapsackHandle.Instance.CatchKnapsack();
            }
            catch (Exception e)
            {
                Logger.InfoFormat("拾取背包物资错误：：{0}", e.Message);
                tmrCheckConnect.Dispose();
                tmrIntervalCheckKnapsackPick = new System.Threading.Timer(CheckKnapsackPickCallBack, null, IntervalCheckKnapsackPick, IntervalCheckKnapsackPick);
                Console.WriteLine("拾取背包物资错误：" + e.Message);
            }
            finally
            {
                tmrIntervalCheckKnapsackPick.Change(IntervalCheckKnapsackPick, IntervalCheckKnapsackPick);
            }
        }

        /// 定时发送背包物资数据
        /// </summary>
        /// <param name="state"></param>
        private void SendKnapsackDataCallBack(object state)
        {
            try
            {

                tmrsendKnapsack.Change(Timeout.Infinite, Timeout.Infinite);
                KnapsackHandle.Instance.RealPushKnapsackData();
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                Logger.Error(e.StackTrace);
                tmrsendKnapsack.Dispose();
                tmrsendKnapsack = new System.Threading.Timer(SendKnapsackDataCallBack, null, IntervalSendKnapsack, IntervalSendKnapsack);
                Console.WriteLine("背包物资更新异常信息：" + e.Message);
            }
            finally
            {
                tmrsendKnapsack.Change(IntervalSendKnapsack, IntervalSendKnapsack);
            }
        }

        #endregion 
        private void StopTimer()
        {
            if (tmrsendPostion != null)
            {
                tmrsendPostion.Dispose();

            }
            if (tmrCheckConnect != null)
            {
                tmrCheckConnect.Dispose();
            }
        }

        ~SeverTimer() // 析构函数
        {

            StopTimer();
        }

    }

    
   
}
